<?php

class StudentQuery_StarController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $this->view->starStyle = array('12'=>'12 zodiac','13'=>'13 zodiac');
        $this->view->starType = $this->_getParam('starType');
        $star13 = array(
         'Sagittarius'=>'1218-0119',
        'Capricorn 13'=>'0120-0217',
        'Aquarius'=>'0218-0312',
        'Pisces'=>'0313-0418',
        'Aries 13'=>'0419-0513',
        'Taurus'=>'0514-0622',
        'Gemini'=>'0623-0721',
        'Cancer'=>'0722-0810',
        'Leo'=>'0811-0916',
        'Virgo'=>'0917-1031',
        'Libra'=>'1101-1123',
        'Scorpio'=>'1124-1129',
        'Ophiuchus'=>'1130-1217');

        $star12 = array(
         'Capricorn'=>'1223-0120',
        'Aquarius'=>'0121-0219',
        'Pisces'=>'0220-0320',
        'Aries'=>'0321-0420',
        'Taurus'=>'0421-0521',
        'Gemini'=>'0522-0621',
        'Cancer'=>'0622-0723',
        'Leo'=>'0724-0823',
        'Virgo'=>'0824-0923',
        'Libra'=>'0924-1023',
        'Scorpio'=>'1024-1122',
        'Sagittarius'=>'1123-1222',
        );
        if ($this->view->starType == 13)
        $this->view->starArr = $star13;
        else
        $this->view->starArr = $star12;

        $year = $this->view->Semester['year'];
        $semester = $this->view->Semester['semester'];
        //取得學生生日資料
        $q = Doctrine_Query::create()
        ->select("a.sn, b.grade AS grade, b.class_sn AS class_sn,
            DATE_FORMAT(a.birthday ,'%m%d') AS md , COUNT(a.sn) AS total")
        ->from('Student_Model_Student a')
        ->leftJoin('a.Student_Model_StudentSemester b')
        ->whereIN('a.study_condition',
        array(Student_Model_StudentTransfer::IN_STADY,
        Student_Model_StudentTransfer::HOMESCHOOLING))
        ->andWhere('b.year=?', $year)
        ->andWhere('b.semester=?', $semester)
        ->groupBy('b.grade, b.class_sn, md');
        $res = $q->fetchArray();
        $arr = array();
        // 月份統計
        $monthTotal = array();
        foreach($res as $val) {
            foreach ($this->view->starArr as $name=>$mon) {

                $tempArr = explode('-',$mon);

                if (! isset($arr[$val['grade'].'-'.$val['class_sn']][$mon] ))
                $arr[$val['grade'].'-'.$val['class_sn']][$mon]  = 0;

                if (! isset($monthTotal[$mon]))
                $monthTotal[$mon] = 0;

                //最初的星座
                if ($tempArr[0] > $tempArr[1]  and
                ($val['md'] >= $tempArr[0] or $val['md'] <= $tempArr[1]) ) {
                    $arr[$val['grade'].'-'.$val['class_sn']][$mon] += $val['total'];
                    $monthTotal[$mon] += $val['total'];
                    break;
                }
                elseif ((int)$val['md'] >= (int)$tempArr[0] and (int)$val['md'] <= (int)$tempArr[1]) {
                    $arr[$val['grade'].'-'.$val['class_sn']][$mon] += $val['total'];
                    $monthTotal[$mon] += $val['total'];
                    break;
                }
            }
        }
        $this->view->monthTotal = $monthTotal;
        $this->view->data = $arr;

        //班級陣列
        $semesterClass = new Semester_Model_SemesterClass();
        $this->view->className = $semesterClass->getClassNames($year,$semester);
    }


}